<template>
  <div class="mod-admin-groupActivity">
    <avue-crud
      ref="crud"
      :page="page"
      :data="dataList"
      :table-loading="dataListLoading"
      :option="tableOption"
      @search-change="searchChange"
      @on-load="getDataList"
      @refresh-change="refreshChange"
    >
      <!-- <template slot-scope="scope" slot="menuLeft"></template> -->
      <template slot-scope="scope" slot="menu">
        <el-button
          icon="el-icon-edit"
          size="small"
          type="text"
          @click="addOrUpdateHandle(scope.row.groupActivityId)"
          >{{ $t("groups.activityDetails") }}</el-button
        >
        <!-- manageGroupProdHandle(scope.row.groupActivityId) -->
        <el-button
          icon="el-icon-s-opportunity"
          size="small"
          type="text"
          @click="selectActivityProd(scope.row.groupActivityId)"
          >{{ $t("groups.viewActivityMerchandise") }}</el-button
        >
        <!-- <el-button icon="el-icon-folder-delete"
                   size="small"
                   type="text"
                   @click="invalidActivityHandle(scope.row)">失效活动

        <el-tooltip class="item"
                    effect="dark"
                    content="“使失效”即立即结束且不可再编辑，未成团订单将自动关闭并退款，已成团订单仍需及时处理。失效后的商品活动可删除。"
                    placement="right">
          <i class="el-icon-question"></i>
        </el-tooltip>
        </el-button>-->
        <el-button
          type="text"
          icon="el-icon-bottom"
          size="small"
          v-if="isAuth('group:activity:update') && scope.row.activityStatus < 3"
          @click="offlineGroupHandle(scope.row)"
          >{{ $t("groups.offShelfActivities") }}</el-button
        >
        <el-tooltip
          class="item"
          effect="dark"
          :content="$t('groups.buttonTips')"
          placement="right"
        >
          <i class="el-icon-question"></i>
        </el-tooltip>
        <el-button
          type="text"
          icon="el-icon-document-checked"
          size="small"
          v-if="
            isAuth('group:activity:update') &&
            scope.row.status > 2 &&
            scope.row.activityStatus !== 3
          "
          @click="groupAuditHandle(scope.row.groupActivityId)"
          >{{
            scope.row.status === 3
              ? $t("coupon.violationOffShelf")
              : $t("coupon.checkPending")
          }}</el-button
        >
        <el-button
          type="text"
          :disabled="
            !(
              scope.row.activityStatus == 1 ||
              scope.row.activityStatus == 3 ||
              scope.row.activityStatus == 4
            )
          "
          icon="el-icon-delete"
          size="small"
          style="color: red"
          @click="deleteHandle(scope.row)"
          >{{ $t("coupon.delete") }}</el-button
        >
      </template>
      <template slot-scope="scope" slot="groupNumber">
        <el-tag size="mini">{{ scope.row.groupNumber }}人团</el-tag>
      </template>
      <template slot-scope="scope" slot="status">
        <el-tag
          type="info"
          v-if="scope.row.status === 1"
          size="mini"
          >{{ $t("groups.startUsing") }}</el-tag
        >
        <el-tag
          v-if="scope.row.status === 2"
          size="mini"
          >{{ $t("groups.notEnabled") }}</el-tag
        >
        <el-tag
          type="success"
          v-if="scope.row.status === 0"
          size="mini"
          >{{ $t("groups.expired") }}</el-tag
        >
        <el-tag
          type="warning"
          v-if="scope.row.status === 3"
          size="mini"
          >{{ $t("groups.offlineViolation") }}</el-tag
        >
        <el-tag
          type="danger"
          v-if="scope.row.status === 4"
          size="mini"
          >{{ $t("groups.platformAudit") }}</el-tag
        >
        <el-tag
          type="danger"
          v-if="scope.row.status === 5"
          size="mini"
          >{{ $t("groups.over") }}</el-tag
        >
        <el-tag
          type="warning"
          v-if="scope.row.status === -1"
          size="mini"
          >{{ $t("weixin.delete") }}</el-tag
        >
      </template>
    </avue-crud>
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="refreshChange"
    ></add-or-update>
    <groupProds
      v-if="groupProdsVisible"
      ref="groupProds"
      @refreshDataList="refreshChange"
    ></groupProds>

    <!-- 商品审核弹窗 -->
    <groupAudit
      v-if="groupAuditVisible"
      ref="groupAudit"
      selectUrl="/platform/group/activity/getOfflineHandleEventByActivityId"
      updateUrl="/platform/group/activity/auditGroupActivity"
      @refreshDataList="refreshChange"
    ></groupAudit>
  </div>
</template>

<script>
import { tableOption } from '@/crud/marketing/groupActivity'
import AddOrUpdate from './groupActivity-add-or-update'
import GroupProds from './groupProd'
import groupAudit from '@/components/offline-audit'
export default {
  data () {
    return {
      dataList: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      searchForm: {},
      dataListLoading: false,
      groupAuditVisible: false,
      groupProdsVisible: false,
      tableOption: tableOption,
      addOrUpdateVisible: false
    }
  },
  components: {
    AddOrUpdate,
    groupAudit,
    GroupProds
  },
  created () {
  },
  mounted () {
  },
  methods: {
    getDataList (page) {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/platform/group/activity/page'),
        method: 'get',
        params: this.$http.adornParams(
          Object.assign(
            {
              current: page == null ? this.page.currentPage : page.currentPage,
              size: page == null ? this.page.pageSize : page.pageSize
            },
            this.searchForm
          )
        )
      }).then(({ data }) => {
        this.dataList = data.records
        this.page.total = data.total
        this.dataListLoading = false
      })
    },
    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    deleteHandle (row) {
      this.$confirm(this.$i18n.t('groups.confirmDelete') + row.activityName + this.$i18n.t('groups.active'), this.$i18n.t('text.tips'), {
        confirmButtonText: this.$i18n.t('coupon.confirm'),
        cancelButtonText: this.$i18n.t('coupon.cancel'),
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl(`/platform/group/activity/${row.groupActivityId}`),
          method: 'delete',
          data: this.$http.adornData({})
        }).then(({ data }) => {
          this.$message({
            message: this.$i18n.t('publics.operation'),
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.refreshChange()
            }
          })
        })
      }).catch(() => { })
    },
    /**
     * 刷新回调
     */
    refreshChange (isRouteToGroupProd, groupActivityId) {
      if (isRouteToGroupProd === 1) {
        this.$router.push({
          path: '/groupProds',
          query: { activityId: groupActivityId }
        })
      }
      this.page = this.$refs.crud.$refs.tablePage.defaultPage
      this.getDataList(this.page)
    },
    searchChange (params) {
      this.searchForm = params
      this.getDataList(this.page)
    },
    // 管理拼团活动商品
    manageGroupProdHandle (groupActivityId) {
      this.visible = false
      this.$router.push({
        path: '/groupProds',
        query: { activityId: groupActivityId }
      })
    },
    // 失效活动活动
    invalidActivityHandle (row) {
      this.$confirm(this.$i18n.t('groups.determinedToFail') + row.activityName + this.$i18n.t('groups.actTip'), this.$i18n.t('text.tips'), {
        confirmButtonText: this.$i18n.t('coupon.confirm'),
        cancelButtonText: this.$i18n.t('coupon.cancel'),
        type: 'warning'
      }).then(
        () => {
          this.$http({
            url: this.$http.adornUrl('/group/activity/invalid/' + row.groupActivityId),
            method: 'PUT',
            data: this.$http.adornData({})
          }).then(({ data }) => {
            this.$message({
              message: this.$i18n.t('publics.operation'),
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList(this.page)
              }
            })
          })
        }).catch(() => { })
    },
    // 下架活动
    offlineGroupHandle (row) {
      var tips = this.$i18n.t('groups.removeActivityNameTips')
      tips = tips.replace('ACTIVITYNAME', row.activityName)
      this.$prompt(tips, this.$i18n.t('text.tips'), {
        confirmButtonText: this.$i18n.t('coupon.confirm'),
        cancelButtonText: this.$i18n.t('coupon.cancel')
      }).then((obj) => {
        if (!obj.value) {
          this.$message.error(this.$i18n.t('groups.offShelfReasonTips'))
          return
        }
        this.$http({
          url: this.$http.adornUrl(`/platform/group/activity/offline`),
          method: 'post',
          data: this.$http.adornData({
            handleId: row.groupActivityId,
            offlineReason: obj.value || null
          })
        }).then(({ data }) => {
          this.$message({
            message: this.$i18n.t('publics.operation'),
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.getDataList(this.page)
            }
          })
        })
      })
    },
    // 弹窗处理
    groupAuditHandle (id) {
      this.groupAuditVisible = true
      this.$nextTick(() => {
        this.$refs.groupAudit.init(id)
      })
    },
    // 弹窗处理
    selectActivityProd (id) {
      this.groupProdsVisible = true
      this.$nextTick(() => {
        this.$refs.groupProds.init(id)
      })
    }
  }
}
</script>
<style lang="scss">
.mod-admin-groupActivity {
}
</style>
